//
//  MBPEX.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/5.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import Foundation
import UIKit
import MBProgressHUD
extension MBProgressHUD{
    
    class func showHUD(text: String, icon: String, view: UIView?){
        if view != nil {
            let hud = MBProgressHUD.showAdded(to: view! , animated: true)
            hud.detailsLabel.text = text
            hud.detailsLabel.font = .systemFont(ofSize: 16)
            hud.customView = UIImageView(image: UIImage(named: "MBProgressHUD.bundle/"+icon))
            hud.mode = .customView
            hud.detailsLabel.textColor = .white
            hud.bezelView.style = .solidColor
            hud.bezelView.color = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
            hud.removeFromSuperViewOnHide = true
            hud.hide(animated: true, afterDelay: 1.5)
        }else{
            if let delegate = UIApplication.shared.delegate as? AppDelegate,
               let window = delegate.window{
                let hud = MBProgressHUD.showAdded(to: window , animated: true)
                hud.detailsLabel.text = text
                hud.detailsLabel.font = .systemFont(ofSize: 16)
                hud.customView = UIImageView(image: UIImage(named: "MBProgressHUD.bundle/"+icon))
                hud.mode = .customView
                hud.detailsLabel.textColor = .white
                hud.bezelView.style = .solidColor
                hud.bezelView.color = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
                hud.removeFromSuperViewOnHide = true
                hud.hide(animated: true, afterDelay: 1.5)
            }
        }
    }
    class func showSuccess(success: String){
        showSuccessToView(success: success, view: nil)
    }
     
    class func showSuccessToView(success: String,view: UIView?){
        showHUD(text: success, icon: "success.png", view: view)
    }
    class func showError(error: String){
        showErrorToView(error: error, view: nil)
    }
    class func showErrorToView(error: String, view: UIView?){
        showHUD(text: error, icon: "error.png", view: view)
    }
    public class func showMessage(message: String) -> MBProgressHUD{
        return self.showMessageToView(message: message, view: nil)
    }
    public class func showMessageToView(message: String, view: UIView?) -> MBProgressHUD{
        var hud = MBProgressHUD()

        if view != nil {
            hud = MBProgressHUD.showAdded(to: view! , animated: true)
            hud.label.text = message
            hud.removeFromSuperViewOnHide = true
            
        }else{
            if let delegate = UIApplication.shared.delegate as? AppDelegate,
               let window = delegate.window{
                hud = MBProgressHUD.showAdded(to: window , animated: true)
                hud.label.text = message
                hud.removeFromSuperViewOnHide = true
            }
        }
        hud.hide(animated: true, afterDelay: 15)
        return hud
    }
    class func hideHUD(){
        if let delegate = UIApplication.shared.delegate as? AppDelegate,
           let window = delegate.window{
            hideHUDForView(view: window)
        }
        
    }
    class func hideHUDForView(view: UIView?){
        if view != nil{
            hide(for: view!, animated: true)
        }else{
            if let delegate = UIApplication.shared.delegate as? AppDelegate,
               let window = delegate.window{
                hide(for: window, animated: true)
            }
            
        }
    }
    
}
